/**
 * @author luzeyu & liangqiaowei
 */
package tiei.ajp.huffman.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileStreamUtils {
    int readIndex;
    int writeIndex;

    // 写入整数
    public static void writeInt(OutputStream out, long N) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int k = 0;
        long temp = N;
        while (temp > 0) {
            buffer.write((int) (temp % 256));
            temp /= 256;
            k++;
        }
        out.write(k);
        buffer.writeTo(out);
    }

    // 读取整数
    public static long readInt(InputStream in) throws IOException {
        int k = in.read();
        long N = 0;
        for (int i = 0; i < k; i++) {
            int byteValue = in.read();
            N += (byteValue & 0xFF) * Math.pow(256, i);
        }
        return N;
    }
//    public static boolean checkTreeEnd(InputStream in){
//
//    }

}
